<!--
 * @Author: wjk
 * @Date: 2020-06-30 14:40:20
 * @LastEditors: wjk
 * @LastEditTime: 2020-11-26 14:23:40
 * @description: file content
-->
<template>
  <view>
    <share v-if="showShareDia" ref="drawImage" :share-types="[1,3,5]" :share-option="shareOption" poster-type="commodity" />
  </view>
</template>
<script>
import { formatMoney } from '@/utils/tools'
import { mapGetters } from 'vuex'
import { navigateTo } from '@/utils/navigateTo'
import { createSharePathUrl } from '@/utils/tools'
import share from '@/components/draw/share'
// #ifdef MP-WEIXIN
// #endif
export default {
  components: {
    share
  },
  props: {
    shareOption: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      showShareDia: false,
      shareUrl: '', // 分享链接
      allData: {} // 传递的数据
    }
  },
  computed: {
    ...mapGetters(['user']),
    vipMoney() {
      if (!this.allData.goods) return 0
      return this.user.current_member_card_type === 3 ? this._formatMoney(this.allData.goods.director_share_commission) : this._formatMoney(this.allData.goods.region_share_commission)
    }
  },
  methods: {
    // 弹出
    async show(allData) {
      if (Object.keys(this.user).length === 0) {
        navigateTo('/pages/login/index')
        return
      }
      this.allData = allData
      if (!allData.share) return
      // 要复制的链接
      const shareUrl = createSharePathUrl({
        share_url: allData.share.url
      })
      // const urlObj = await this.$api.goodsApi.shortUrl({
      //   longurl: shareUrl
      // })
      // this.shareUrl = this.shareOption.share_url = urlObj.data || shareUrl

      this.shareUrl = this.shareOption.share_url = shareUrl
      this.shareOption.share_list = this.getCodeUrl()
      this.showShareDia = true
      this.$nextTick(() => {
        this.$refs.drawImage.show(this.shareOption)
      })
    },
    getCodeUrl(val) {
      const res = this.allData
      const banner_list = res.goods.banner_list.filter(item => item.media_type !== 1)
      if (!banner_list.length) {
        banner_list.push({
          url: res.goods.pic || res.goods.image
        })
      }
      this.shareData = banner_list.map(item => {
        return {
          pic: item.url,
          title: res.share.title || res.goods.title,
          price: res.goods.vip_price,
          minPrice: res.goods.ComboPriceMin,
          maxPrice: res.goods.ComboPriceMax,
          original: res.goods.price,
          url: this.shareUrl
        }
      })
      return this.shareData
    },
    // 关闭
    _formatMoney(price) {
      if (typeof price === undefined || price === '') {
        return 0.00
      }
      return formatMoney(price)
    }
  }

}
</script>
